<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增商品分类</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="edit(scope.row)">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加or修改 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="分类名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addGoodsCategoryInfo, getGoodsCategoryInfoList,
  removeGoodsCategoryInfo, updateGoodsCategoryInfo
} from "@/api/admin/goodsInfo";
export default {
  name: "category",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频管理表格数据
      categoryList: [],
      options: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {}
    };
  },

  created () {
    this.getList();
  },

  methods: {
    /** 查询商品分类列表 */
    getList () {
      this.loading = true;
      getGoodsCategoryInfoList().then((response) => {
        this.categoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 删除按钮操作 */
    handleDelete (row) {
      const id = row.categoryId;
      this.$confirm("确定删除该数据吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return removeGoodsCategoryInfo(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },

    // 修改
    edit (row) {
      this.form = { ...row };
      this.open = true;
      this.title = "修改商品";
    },

    // 新增商品分类
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "新增店铺分类";
    },

    // 表单重置
    reset () {
      this.form = {};
      this.resetForm("form");
    },

    // 取消按钮
    cancel () {
      this.reset();
      this.open = false;
    },

    // 提交
    submitForm () {
      this.$refs["form"].validate((valid) => {
        // 新增
        if (valid) {
          if (this.form.categoryId != undefined) {
            updateGoodsCategoryInfo(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.getList();
                this.cancel();
              }
            });
            //修改
          } else {
            addGoodsCategoryInfo(this.form).then((res) => {
              if (res.code === 200) {
                this.msgSuccess("新增成功");
                this.getList();
                this.cancel();
              }
            });
          }
        }
      });
    },
  },
};
</script>
